﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace Example6_Particles.View
{
    class View 
    {
        private SpriteBatch     m_spriteBatch;
        private Texture2D       m_sparkTexture;
        private Texture2D       m_smokeTexture;
        private KeyboardState   m_oldKeyboardState;
        private float           m_viewScale = 100;
        private float           m_time = 0;
        private float            m_explosionTimer = 0;

        private ExplosionSystem m_explosion = new ExplosionSystem();
        private SmokeSystem m_smoke = new SmokeSystem();
        private StatefullSpraySystem m_spray = new StatefullSpraySystem();
        private ExampleParticleSystem m_particles = new ExampleParticleSystem();

        private MouseState m_oldMousePos = Mouse.GetState();
        
        

        public View(GraphicsDeviceManager a_manager, ContentManager a_contentLoader)
        {
            //Save start key state to detect changes
            m_oldKeyboardState = Keyboard.GetState();

            //this is needed to draw sprites
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);

            //Load content
            m_sparkTexture = a_contentLoader.Load<Texture2D>("spark");
            m_smokeTexture = a_contentLoader.Load<Texture2D>("particlesmoke");
        }

        

        internal void DrawLevel(Microsoft.Xna.Framework.Graphics.GraphicsDevice a_graphicsDevice, float a_elapsedTime)
        {
            //draw green background
            a_graphicsDevice.Clear(Microsoft.Xna.Framework.Color.White);

            //draw all images
            m_spriteBatch.Begin();

            //m_particles.Draw(m_spriteBatch, m_sparkTexture, a_elapsedTime);

            //update the smoke
            m_time += a_elapsedTime / 10.0f;


            //The explosion happens at m_explosionTimer = 0.0f
            /*m_explosionTimer += a_elapsedTime / 20;
            if (m_explosionTimer > 2.0f)
            {
                m_explosionTimer = 0.0f;
            }*/

            //place the system in the middle of the screen
            Vector2 displacement = new Vector2(a_graphicsDevice.Viewport.Width / 2, a_graphicsDevice.Viewport.Height / 2);

            //Draw Explosion
            //m_explosion.DrawParticleSystem(m_explosionTimer, displacement - new Vector2(150.0f, 0), m_viewScale, m_spriteBatch, m_sparkTexture);

            //Draw smoke
           // m_smoke.DrawSmoke(m_time, displacement, m_viewScale, m_spriteBatch, m_smokeTexture);

            //And another smokesystem
            //m_smoke.DrawSmoke(m_time*4.0f, displacement + new Vector2(150.0f, 0), m_viewScale, m_spriteBatch, m_smokeTexture);


            Vector2 mouseModelCoordinates = new Vector2(Mouse.GetState().X, Mouse.GetState().Y) / m_viewScale;
            Vector2 mouseModelSpeed = new Vector2((Mouse.GetState().X - m_oldMousePos.X) / m_viewScale, 
                                                  (Mouse.GetState().Y - m_oldMousePos.Y) / m_viewScale);

            m_oldMousePos = Mouse.GetState();
            //Draw the statefull system
            m_spray.DrawSpray(m_time, mouseModelCoordinates, mouseModelSpeed, new Vector2(0, 0), m_viewScale, m_spriteBatch, m_sparkTexture);
              
            
             

            m_spriteBatch.End();

            
        }

        
    }
}
